/*
 * PopupWindows.java
 *
 * @author Vladimir Krukov
 */

package chatty.ui;

import DrawControls.*;
import DrawControls.text.*;
import javax.microedition.lcdui.*;
import chatty.*;
import chatty.cl.*;
import chatty.comm.*;
import chatty.modules.*;
import chatty.ui.base.*;
import chatty.ui.timers.*;
import chatty.util.*;
import protocol.Contact;

/**
 * 
 * @author Vladimir Krukov
 */
public class PopupWindow extends CanvasEx implements Runnable {
    private int x;
    private int y;
    private int width;
    private int activeHeight;

    private String text;
    private String title;

    private int curPos;
    private Par par;

    public static final byte TYPE_MESSAGE     = 1;
    public static final byte TYPE_NEW_VERSION = 2;
    public static final byte TYPE_NOTICE      = 3;
    public static final byte TYPE_YESNO       = 4;
    private byte type;
    private PopupListener popupListener;

    // #sijapp cond.if target is "MIDP2" | target is "MOTOROLA"#
    public static PopupWindow createNewVersion() {
        String title = "check_updates";
        String msg = ResBundle.getString("new_version_available");
        String lastVer = Options.getString(Options.OPTION_LAST_VERSION);
        msg = Util.replace(msg, "###LASTVER###", lastVer);
        return new PopupWindow(title, msg, TYPE_NEW_VERSION);
    }
    // #sijapp cond.end#
    public void setPopupListener(PopupListener l) {
        popupListener = l;
    }

    public PopupWindow(String title, String text, byte type) {
        this.type = type;
        this.title = ResBundle.getString(title);
        this.text  = StringConvertor.notNull(text);

        if (TYPE_YESNO == type) {
            setSoftBarLabels("yes", "no");
        } else {
            String left = (TYPE_NEW_VERSION == type) ? "update" : null;
            setSoftBarLabels(left, "close");
        }
    }
    public PopupWindow(String title, String text) {
        this(title, text, TYPE_NOTICE);
    }
    
    public static void showAttentionBox(String text, PopupListener listener) {
        String cap = ResBundle.getString("attention");
        PopupWindow msgForm = new PopupWindow(cap, text, PopupWindow.TYPE_YESNO);
        msgForm.setPopupListener(listener);
        msgForm.show();
    }
    public static PopupWindow showShadowPopup(String title, String text) {
        PopupWindow win = new PopupWindow(title, text, TYPE_MESSAGE);
        synchronized (NativeCanvas.setDisplaySemaphore) {
            Object o = Chatty.getCurrentDisplay();
            if (o instanceof PopupWindow) {
                win.activeHeight = -1;
                PopupWindow top = (PopupWindow) o;
                PopupWindow p = top;
                while (p.prevDisplay instanceof PopupWindow) {
                    p = (PopupWindow)p.prevDisplay;
                }
                win.prevDisplay = p.prevDisplay;
                p.prevDisplay = win;
                top.invalidate();
                return win;
            }
        }
        win.show();
        return win;
    }

    protected void stylusTap(int x, int y, boolean longTap) {
        back();
    }
    
    protected int getHeight() {
        return activeHeight;
    }
    
    protected int getWidth() {
        return width;
    }

    private int getMaxHeight() {
        int height = getScreenHeight();
        return (height <= 200) ? height - 15 : height * 6 / 10;
    }
    private void setActiveHeight(int h) {
        activeHeight = h;
        y = getScreenHeight() - activeHeight - (getMaxHeight() == h ? 1 : 0);
    }
    protected void beforShow() {
        width  = getScreenWidth() - 4;
        x = getScreenWidth() - width - 2;

        if (0 == activeHeight) {
            setActiveHeight(getMaxHeight() * 5 / 10);
            new Thread(this).start();
        } else {
            setActiveHeight(getMaxHeight());
        }

        Font[] set = GraphicsEx.popupFontSet;
        Parser parser = new Parser(-1, set, width - 7);
        parser.addBigText(text, THEME_POPUP_TEXT, FONT_STYLE_PLAIN, true);
        par = parser.getPar();
        curPos = 0;
    }

    private int getTextHeight() {
        Font[] set = GraphicsEx.popupFontSet;
        return par.getHeight() + 8
                + (set[FONT_STYLE_PLAIN].getHeight() + 1) * 2;
    }

    protected void paint(GraphicsEx g) {
        int width = getWidth();
        int height = getHeight();

        Object o = prevDisplay;
        int i = 1;
        while (o instanceof PopupWindow) {
            i++;
            o = ((PopupWindow)o).prevDisplay;
        }
        paintBack(g, o);
        g.setClip(x, y, width, height);
        g.setStrokeStyle(Graphics.SOLID);

        Font font = GraphicsEx.popupFontSet[FONT_STYLE_PLAIN];
        g.setFont(font);
        int capHeight = font.getHeight() + 1;
        int contentX = x + 3;
        int contentY = y + 3 + capHeight;

        g.fillRect(x, y, width, height, THEME_POPUP_BACK);
        g.drawDoubleBorder(x, y, width, height, THEME_POPUP_BORDER);

        int capBkCOlor = g.getThemeColor(THEME_POPUP_BACK);
        g.fillGradRect(capBkCOlor, g.transformColorLight(capBkCOlor, -32), x + 1, y + 1, width - 1, capHeight - 1);
        g.setThemeColor(THEME_POPUP_BORDER);
        g.drawLine(x, y + capHeight, x + width, y + capHeight);

        g.setThemeColor(THEME_POPUP_TEXT);
        String caption = title;
        if (1 < i) {
            caption = Integer.toString(i) + ") " + caption;
        }
        g.drawString(null, caption, null, contentX, y, width - 6, capHeight);
        showText(g, contentX, contentY, width - 7, height - 6 - capHeight, curPos);
    }
	private void showText(GraphicsEx g, int x, int y, int width, int height, int skipHeight) {
        Font[] fontSet = GraphicsEx.popupFontSet;
        int clipX = g.getClipX();
        int clipY = g.getClipY();
        int clipHeight = g.getClipHeight();
        int clipWidth = g.getClipWidth();
        g.clipRect(x, y, width, height);

        par.paint(fontSet, g, x, y - skipHeight, skipHeight, height, false);

        g.setClip(clipX, clipY, clipWidth, clipHeight);
	}
    


    protected void doKeyReaction(int keyCode, int actionCode, int type) {
		switch (actionCode) {
            case NativeCanvas.NAVIKEY_DOWN:
            case NativeCanvas.NAVIKEY_UP:
                curPos += (actionCode == NativeCanvas.NAVIKEY_DOWN) ? STEP : -STEP;
                curPos = Math.max(0, Math.min(curPos, getTextHeight() - getHeight()));
                invalidate();
                return;
        }
        if (KEY_PRESSED != type) {
            return;
        }
        switch (keyCode) {
            case NativeCanvas.LEFT_SOFT:
                // #sijapp cond.if target is "MIDP2" | target is "MOTOROLA"#
                if (TYPE_NEW_VERSION == type) {
                    GetVersion.updateProgram();
                    return;
                }
                // #sijapp cond.end#
                if (null != popupListener) {
                    popupListener.popupAction();
                    return;
                }
                Object o = prevDisplay;
                while (o instanceof PopupWindow) {
                    Object obj = ((PopupWindow)o).prevDisplay;
                    ((PopupWindow)o).prevDisplay = null;
                    o = obj;
                }
                Chatty.setDisplay(o);
                return;
            case NativeCanvas.RIGHT_SOFT:
            case NativeCanvas.CLOSE_KEY:
            case NativeCanvas.CLEAR_KEY:
                back();
                return;
        }
        // #sijapp cond.if modules_DEBUGLOG is "true" #
        try {
            String strCode = NativeCanvas.getInstance().getKeyName(keyCode);
            DebugLog.println("key = '" + strCode + "'(" + keyCode + ")");
        } catch(IllegalArgumentException e) {
            DebugLog.println("key = 'null'(" + keyCode + ")" + actionCode);
        }
        // #sijapp cond.end #
    }
    
    private static final int STEP = 10;

    public void run() {
        try { Thread.sleep(20); } catch (Exception e) {}
        setActiveHeight(getMaxHeight() * 9 / 10);
        invalidate();
        try { Thread.sleep(50); } catch (Exception e) {}
        setActiveHeight(getMaxHeight());
        invalidate();
    }
}